package com.registry.center.config;

import com.netflix.appinfo.InstanceInfo;
import org.springframework.cloud.netflix.eureka.server.event.*;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 类描述：eureka的各种服务的注册、下线等的监听事件
 */
@Component
public class EurekaStateChangeListener {

    /**
     * 功能描述：服务断线以后的通知事件
     * @param eurekaInstanceCanceledEvent
     */
    @EventListener
    public void listen(EurekaInstanceCanceledEvent eurekaInstanceCanceledEvent) {
        //服务断线事件
        String appName = eurekaInstanceCanceledEvent.getAppName();
        String serverId = eurekaInstanceCanceledEvent.getServerId();
        System.out.println("断线事件：【serverId：【"+serverId+"】；appName：【"+appName+"】已经断线了！】");
    }

    /**
     * 功能描述：注册的时候监听事件
     * @param event
     */
    @EventListener
    public void listen(EurekaInstanceRegisteredEvent event) {
        InstanceInfo instanceInfo = event.getInstanceInfo();
        System.out.println("注册事件：【instanceInfo：【"+instanceInfo+"】】");
    }


    /**
     * 功能描述：重连事件
     * @param event
     */
    @EventListener
    public void listen(EurekaInstanceRenewedEvent event) {
        String appName = event.getAppName();
        String serverId = event.getServerId();
        System.out.println("重连事件：【appName：【"+appName+"】；serverId："+serverId+"】");
    }

    @EventListener
    public void listen(EurekaRegistryAvailableEvent event) {

    }

    @EventListener
    public void listen(EurekaServerStartedEvent event) {
        //Server启动
    }


}
